/*
 The circuit:
 LCD RS pin to digital pin 12
 LCD Enable pin to digital pin 11
 LCD D4 pin to digital pin 5
 LCD D5 pin to digital pin 4
 LCD D6 pin to digital pin 3
 LCD D7 pin to digital pin 2
 
 LCD R/W pin to ground
 LCD VSS pin to ground
 LCD VCC pin to 5V
 10K resistor:
 ends to +5V and ground
 wiper to LCD VO pin (pin 3)
 */

#include <LiquidCrystal.h>
#include <DS1302.h>
const int kCePin   = 9;  // Chip Enable
const int kIoPin   = 11;  // Input/Output
const int kSclkPin = 10;  // Serial Clock
const int button = 8;

DS1302 rtc(kCePin, kIoPin, kSclkPin);
LiquidCrystal lcd(1, 2, 3, 4, 6, 7);
Time t = rtc.time();
void setup() {
  pinMode(button,INPUT);
  rtc.writeProtect(false);
  rtc.halt(false);
  lcd.begin(16, 2);
}

void loop() {
  if(digitalRead(button))
  {
    int time = millis();
    while(digitalRead(button))
    {
      lcd.clear();
      showTemp();
    }
    time = millis() - time;
    if(time>=1500)
      setTime();
    lcd.clear();
  }
  t=rtc.time();
  showTime();
}

void showTime()
{
  lcd.setCursor(3,0);
  lcd.print(t.yr);
  lcd.setCursor(7,0);
  lcd.print('-');
  lcd.setCursor(8,0);
  if(t.mon<10)
  {
    lcd.print('0');
    lcd.setCursor(9,0);
  }    
  lcd.print(t.mon);
  lcd.setCursor(10,0);
  lcd.print('-');
  lcd.setCursor(11,0);
  if(t.date<10)
  {
    lcd.print('0');
    lcd.setCursor(12,0);
  } 
  lcd.print(t.date);

  lcd.setCursor(4,1);
  if(t.hr<10)
  {
    lcd.print('0');
    lcd.setCursor(5,1);
  } 
  lcd.print(t.hr);
  lcd.setCursor(6,1);
  lcd.print(':');
  lcd.setCursor(7,1);
  if(t.min<10)
  {
    lcd.print('0');
    lcd.setCursor(8,1);
  } 
  lcd.print(t.min);
  lcd.setCursor(9,1);
  lcd.print(':');
  lcd.setCursor(10,1);
  if(t.sec<10)
  {
    lcd.print('0');
    lcd.setCursor(11,1);
  } 
  lcd.print(t.sec);
  delay(500);
}

void showTemp()
{
  int val;//定义变量
  float dat;//定义变量
  val = analogRead(0); // 读取传感器的模拟值并赋值给val
  dat = (float)val * (5.0 / 1023.0 * 100)-5; //温度计算公式

  lcd.setCursor(0, 0);
  lcd.print("Show me the temp");

  lcd.setCursor(5, 1);
  lcd.print(dat);

  lcd.setCursor(10, 1);
  lcd.print('C');
  delay(500);
}

void setTime()
{
  boolean ok = false;
  lcd.clear();
  delay(500);
  showTime();
  while(!ok)
  {
    boolean year_ok = false;
    boolean month_ok = false;
    boolean day_ok = false;
    boolean hour_ok = false;
    boolean min_ok = false;
    boolean sec_ok = false;
    while(!year_ok)
    {
      if(digitalRead(button))
      {
        int time = millis();
        while(digitalRead(button));
        time = millis()-time;
        if(time<500)
        {
          t.yr++;
          showTime();
        }
        else if(time<1500)
        {
          t.yr--;
          showTime();
        }
        else
        {
          lcd.clear();
          delay(500);
          showTime();
          year_ok = true;
        }
      }
    }

    while(!month_ok)
    {
      if(digitalRead(button))
      {
        int time = millis();
        while(digitalRead(button));
        time = millis()-time;
        if(time<500)
        {
          t.mon++;
          if(t.mon>12)
          t.mon=12;
          showTime();
        }
        else if(time<1500)
        {
          t.mon--;
          if(t.mon<0)
          t.mon = 0;

          showTime();
        }
        else
        {
          lcd.clear();
          delay(500);
          showTime();
          month_ok = true;
        }
      }
    }  
    while(!day_ok)
    {
      if(digitalRead(button))
      {
        int time = millis();
        while(digitalRead(button));
        time = millis()-time;
        if(time<500)
        {
          t.date++;
          if(t.date>31)
          t.date = 31;
          showTime();
        }
        else if(time<1500)
        {
          t.date--;
          if(t.date<0)
          t.date = 0;
          showTime();
        }
        else
        {
          lcd.clear();
          delay(500);
          showTime();
          day_ok = true;
        }
      }
    }  
    while(!hour_ok)
    {
      if(digitalRead(button))
      {
        int time = millis();
        while(digitalRead(button));
        time = millis()-time;
        if(time<500)
        {
          t.hr++;
          if(t.hr>=24)
          t.hr=0;
          showTime();
        }
        else if(time<1500)
        {
          t.hr--;
          if(t.hr<0)
            t.hr=0;
          showTime();
        }
        else
        {
          lcd.clear();
          delay(500);
          showTime();
          hour_ok = true;
        }
      }
    }
    while(!min_ok)
    {
      if(digitalRead(button))
      {
        int time = millis();
        while(digitalRead(button));
        time = millis()-time;
        if(time<500)
        {
          t.min++;
          if(t.min>=60)
          t.min=0;
          showTime();
        }
        else if(time<1500)
        {
          t.min--;
          if(t.min<0)
          t.min=0;
          showTime();
        }
        else
        {
          lcd.clear();
          delay(500);
          showTime();
          min_ok = true;
        }
      }
    }
    ok = true;  
  }
  rtc.time(t);
}









